package com.huxiu.fragments;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.huxiu.R;
import com.huxiu.base.BaseFragment;
import com.huxiu.base.BasePager;
import com.huxiu.pagers.MyPager;
import com.huxiu.pagers.NewscenterPager;
import com.huxiu.pagers.QunZuPager;
import com.huxiu.utils.Constants;
import com.huxiu.utils.UmenEvent;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class ContentFragment extends BaseFragment {

    private ViewPager vp_content_pager;
    private List<BasePager> pagers;
    private RadioGroup rg_content_bottom;

    @Override
    protected View initView() {
        View view = View.inflate(mContext, R.layout.content, null);
        vp_content_pager = (ViewPager) view.findViewById(R.id.vp_content_pager);
        rg_content_bottom = (RadioGroup) view.findViewById(R.id.rg_content_bottom);
        return view;
    }

    @Override
    protected void initData() {
        // 创建5个pager对象

        pagers = new ArrayList<BasePager>();
        pagers.add(new NewscenterPager(mContext));
        pagers.add(new QunZuPager(mContext));
        pagers.add(new MyPager(mContext));
        // 更新ViewPager
        vp_content_pager.setAdapter(new MyAdapter());
        // 监听ViewPager
        vp_content_pager.setOnPageChangeListener((ViewPager.OnPageChangeListener) new MyOnPageChangeListener());
        // 初始化首页，加载数据
        pagers.get(0).initData();
        // 监听底部单选按钮
        rg_content_bottom.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        // 设置默认选中首页
        rg_content_bottom.check(R.id.rb_home);
    }

    class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_home:

                    vp_content_pager.setCurrentItem(0, false);
                    break;
                case R.id.rb_qunzu:

                    vp_content_pager.setCurrentItem(1, false);
                    break;
                case R.id.rb_me:

                    vp_content_pager.setCurrentItem(2, false);
                    break;
                default:
                    break;
            }
        }
    }


    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            // 当用户切换到某一页时，再加载数据
            switch (position) {
                case 0:
                    UmenEvent.UmengEventMap(mContext, UmenEvent.APP_HOME, UmenEvent.HOME_TABBAR_ARTICLE);
//                    MobclickAgent.onEvent(mContext, UmenEvent.HOME_TABBAR_ARTICLE);
                    break;
                case 1:
                    UmenEvent.UmengEventMap(mContext,UmenEvent.APP_HOME,UmenEvent.HOME_TABBAR_GROUP);
//                    MobclickAgent.onEvent(mContext, UmenEvent.HOME_TABBAR_GROUP);
                    break;
                case 2:
                    UmenEvent.UmengEventMap(mContext,UmenEvent.APP_HOME,UmenEvent.HOME_TABBAR_USERCENTER);
//                    MobclickAgent.onEvent(mContext, UmenEvent.HOME_TABBAR_USERCENTER);
                    break;
                default:
                    break;
            }
            pagers.get(position).initData();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }

    class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager basePager = pagers.get(position);
            // 返回控件
            container.addView(basePager.rootView);
            // 更新界面的数据，预加载功能，防止加载数据浪费流量
            //			basePager.initData();
            return basePager.rootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    // 对外返回获取新闻中心对象的方法
    public NewscenterPager getNewscenterPager() {
        return (NewscenterPager) pagers.get(1);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainActivity");
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainActivity");
    }
}
